package org.jeecg.modules.business.third.strategy.emsdms;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.business.third.service.IThirdCommonService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.dock.entity.mds.MdsBaseResponseVO;
import org.jeecg.modules.dock.entity.mds.request.EmptyMaterialRackRequestVO;
import org.jeecg.modules.dock.entity.mds.request.MDSCrossShearingToWithdrawMaterialRequestVO;
import org.jeecg.modules.dock.entity.mes.request.CrossShearingMaterialWithdrawalRequestVO;
import org.jeecg.modules.dock.enums.MdsStatusEnum;
import org.jeecg.modules.dock.service.MdsDockingRepeaterService;
import org.jeecg.modules.flow.sys.constant.FlowConstant;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    横剪退料(条料) 14 dms
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r14DmsWarehouse")
@RequiredArgsConstructor
@Slf4j
public class R14DmsWarehouse implements ThirdOperateStrategy {

    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final MdsDockingRepeaterService mdsDockingRepeaterService;
    private final IThirdCommonService thirdCommonService;
    private final IWmsInReceiptTaskService iWmsInReceiptTaskService;


    @Override
    public boolean operate(Map<String, Object> map) {
        Integer type = (Integer) map.get("type");
        AtomicBoolean result = new AtomicBoolean(true);
        try {
            if (ObjectUtils.compare(type, 0) == 0) {
                Optional<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialsOptional = thirdCommonService.getWmsInReceiptTaskMaterials(map.get("taskId").toString());
                if (wmsInReceiptTaskMaterialsOptional.isPresent()) {
                    WmsInReceiptTaskMaterial taskMaterial = wmsInReceiptTaskMaterialsOptional.get();
                    CrossShearingMaterialWithdrawalRequestVO vo = JSONObject.parseObject(taskMaterial.getExtraText(), CrossShearingMaterialWithdrawalRequestVO.class);
                    MDSCrossShearingToWithdrawMaterialRequestVO request = new MDSCrossShearingToWithdrawMaterialRequestVO();
                    request.setTaskCode(vo.getTaskCode());
                    request.setOperateTime(new Date());
                    request.setTaskType(vo.getTaskType());
                    request.setTaskStatus(MdsStatusEnum.TASK_RECEIVED.getCode());
                    request.setSite(vo.getSite());
                    request.setOperatorName(vo.getOperatorName());
                    request.setLineName(vo.getLineName());
                    request.setOrderCode(vo.getOrderCode());
                    WmsInReceiptTask task = iWmsInReceiptTaskService.getById(taskMaterial.getWmsInReceiptTaskId());
                    if (ObjectUtil.isNotNull(task)) {
                        request.setPrepareArea(task.getMaterialArea());
                    }
                    if (ObjectUtil.isNotNull(vo.getDetail())) {
                        List details = vo.getDetail().stream().map(o -> (Object) o).collect(Collectors.toList());
                        request.setDetail(details);
                    }
                    MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.mdsCrossShearingToWithdrawMaterial(request);
                    if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                        if (FlowConstant.SUCCESS.equals(mdsBaseResponseVO.getTarget())) {
                            if (ObjectUtils.isNotEmpty(mdsBaseResponseVO.getData())) {
                                taskMaterial.setExtraResponse(JSON.toJSONString(mdsBaseResponseVO.getData()));
                                wmsInReceiptTaskMaterialService.updateById(taskMaterial);
                            }
                        }
                        if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                            result.set(false);
                        }
                    } else {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            }
            if (ObjectUtils.compare(type, 2) == 0) {
                Optional<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialsOptional = thirdCommonService.getWmsInReceiptTaskMaterials(map.get("taskId").toString());
                if (wmsInReceiptTaskMaterialsOptional.isPresent()) {
                    WmsInReceiptTaskMaterial taskMaterial = wmsInReceiptTaskMaterialsOptional.get();
                    CrossShearingMaterialWithdrawalRequestVO vo = JSONObject.parseObject(taskMaterial.getExtraText(), CrossShearingMaterialWithdrawalRequestVO.class);
                    EmptyMaterialRackRequestVO request = new EmptyMaterialRackRequestVO();
                    request.setTaskCode(vo.getTaskCode());
                    request.setOperateTime(new Date());
                    request.setTaskType(vo.getTaskType());
                    request.setSite(vo.getSite());
                    request.setOperatorName(vo.getOperatorName());
                    request.setTaskStatus(MdsStatusEnum.RETURN_PALLET_READY.getCode());
                    String currentSite = "";
                    if (StringUtils.isNotBlank(taskMaterial.getExtraResponse())) {
                        JSONArray extraResponse = JSONArray.parseArray(taskMaterial.getExtraResponse());
                        JSONObject jsonObject = extraResponse.getJSONObject(0);
                        Object site = jsonObject.get("currentSite");
                        if (ObjectUtils.isNotEmpty(site)) {
                            currentSite = String.valueOf(site);
                        }
                    }
                    request.setCurrentSite(currentSite);
                    request.setLineName(vo.getLineName());
                    MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.emptyMaterialRack(request);
                    if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                        if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                            result.set(false);
                        }
                    } else {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            }
        } catch (Exception e) {
            log.error("R14DmsWarehouse:异常，{}", e.getMessage(), e);
            result.set(false);
        }
        return result.get();
    }
}
